package servlets;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import domain.Contact;
import domain.IDAOContact;

public class deleteContactAction extends Action {
	
	public ActionForward execute(final ActionMapping pMapping, ActionForm pForm, final HttpServletRequest pRequest, final HttpServletResponse pResponse) {
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(pRequest.getSession().getServletContext());
		IDAOContact dao = (IDAOContact) context.getBean("daoContact");
		boolean result = dao.deleteContact(Long.parseLong(pRequest.getParameter("id")));
		pRequest.setAttribute("type", "show");
		List<Contact> contacts = dao.getAllContact();
		pRequest.setAttribute("contacts", contacts);
		
        if (result) {
        	pRequest.setAttribute("contactDeleted", true);
		} else {
			pRequest.setAttribute("contactDeleted", false);
		}        
        
        return pMapping.findForward("resultsPage");
		
	}
}